$(function() {
    aa()

    //获取分类数据
    function aa() {
        $.ajax(baseUrl + '/users', {
            success: function(res) {
                const newHtml = template('userlist_id', { userlist: res, baseUrl: baseUrl })
                $('.table tbody').html(newHtml)
            },
            error: function(err) {
                console.log('err', err);
            }
        });
    }

    //添加用户
    $('.btn-primary').click(function() {
        let text = {
            nickName: $('#name').val(),
            email: $('#email').val(),
            password: $("#pwd").val(),
            role: 'admin',
            avatar: $('#imgs').attr('src'),
            status: 1
        }
        console.log('------', text);
        $.ajax(baseUrl + '/users', {
            async: false,
            type: 'POST',
            data: text,
            success: function(res) {
                aa()
                console.log(res);
            },
            error: function(err) {
                console.log('err', err);
            }
        });
    })

    //删除用户
    $('tbody').on('click', '.dell', function() { //父类委托  
        console.log();
        $.ajax(baseUrl + '/users/' + $(this).attr('key'), {
            async: false,
            type: 'DELETE',
            success: function(res) {
                aa()
                console.log(res);
            },
            error: function(err) {
                console.log('err', err);
            }
        });
    })
})